package com.example.first.activities;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.first.R;
import com.example.first.adapters.HistoryOrderAdapter;
import com.example.first.modle.ApiResponse;
import com.example.first.modle.OrderVO;
import com.example.first.services.ApiService;
import com.example.first.utils.RetrofitClient;

import java.util.List;

import retrofit2.Call;

public class HistoryOrderActivity extends AppCompatActivity {

    ImageButton back;


    ListView orderList;
    TextView allOrder;
    TextView waitOrder;
    TextView cancelOrder;
    View allOrderView;
    View waitOrderView;
    View cancelOrderView;

    private List<OrderVO> orders;

    private HistoryOrderAdapter adapter;

    private Integer type=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_order);

        // 设置 Toolbar 标题
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("外卖订餐");
        }


        // 设置状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor("#FFC300")); // 设置为你想要的颜色
        }


        //返回
        back = findViewById(R.id.back);
        back.setOnClickListener(v -> finish());


        allOrder=findViewById(R.id.all_order);
        waitOrder=findViewById(R.id.wait_order);
        cancelOrder=findViewById(R.id.cancel_order);
        orderList=findViewById(R.id.order_list);
        allOrderView=findViewById(R.id.all_order_line);
        waitOrderView=findViewById(R.id.wait_order_line);
        cancelOrderView=findViewById(R.id.cancel_order_line);

        //全部订单点击事件
        allOrder.setOnClickListener(v -> {
            if(type==null){
                return;
            }
            type=null;
            allOrderView.setBackgroundColor(getResources().getColor(R.color.yellow));
            waitOrderView.setBackgroundColor(getResources().getColor(R.color.white));
            cancelOrderView.setBackgroundColor(getResources().getColor(R.color.white));
            getOrderList();
        });

        //待支付点击事件
        waitOrder.setOnClickListener(view -> {
            if (type!=null&&type==1){
                return;
            }
            type=1;
            allOrderView.setBackgroundColor(getResources().getColor(R.color.white));
            waitOrderView.setBackgroundColor(getResources().getColor(R.color.yellow));
            cancelOrderView.setBackgroundColor(getResources().getColor(R.color.white));
            getOrderList();
        });

        //已取消点击事件
        cancelOrder.setOnClickListener(view -> {
            if (type!=null&&type==6){
                return;
            }
            type=6;
            allOrderView.setBackgroundColor(getResources().getColor(R.color.white));
            waitOrderView.setBackgroundColor(getResources().getColor(R.color.white));
            cancelOrderView.setBackgroundColor(getResources().getColor(R.color.yellow));
            getOrderList();
        });

        getOrderList();



    }

    public void getOrderList(){
        ApiService apiService= RetrofitClient.getApiService();
        Call<ApiResponse<List<OrderVO>>> call=apiService.getHistoryOrders(type);
        call.enqueue(new retrofit2.Callback<ApiResponse<List<OrderVO>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<OrderVO>>> call, retrofit2.Response<ApiResponse<List<OrderVO>>> response) {
                if (response.isSuccessful() && response.body()!=null){
                    if (response.body().getCode()==1){
                        //成功
                        orders=response.body().getData();
                        adapter=new HistoryOrderAdapter(HistoryOrderActivity.this,orders);
                        orderList.setAdapter(adapter);

                    }else {
                        Toast.makeText(HistoryOrderActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    }

                }else {

                    Toast.makeText(HistoryOrderActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ApiResponse<List<OrderVO>>> call, Throwable t) {
                Toast.makeText(HistoryOrderActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}